#include <data.h>
#include <tool.h>
#include <method_turtle.h>
#include <stdio.h>
void turtle(DATA data[], int datanum, TurtleType type, int atr, int BreakOutAddRange, int BreakOutDelRange)
{
	int i;
	double d1;

	for(i=0; i<datanum; i++)
	{
		data[i].money = 0;
		data[i].have = 0;
		data[i].tr = 0;
		data[i].atr = 0;
		data[i].add1 = 0;
		data[i].add2 = 0;
		data[i].add3 = 0;
		data[i].add4 = 0;
		data[i].del1 = (type==TURTLE_BUY)?0:9999;
		data[i].del2 = 0;
	}
	
	// TR
	for(i=datanum-1; i>=0; i--)
		data[i].tr = TureRange(data, datanum, i);

	// ATR
	for(i=datanum-1; i>=0; i--)
		data[i].atr = AverageTrueRange(data, datanum, i, atr);

	// add1234 & dell
	for(i=datanum-1; i>=0; i--)
	{
		if( (data[i].add1=BreakOut(data, datanum, i, BreakOutAddRange, (type==TURTLE_BUY)?(BREAKOUT_HIGH):(BREAKOUT_LOW))) > 0 )
		{
			data[i].add2 = data[i].add1 + ((type==TURTLE_BUY)?1:-1) * data[i].atr * 0.5;
			data[i].add3 = data[i].add1 + ((type==TURTLE_BUY)?1:-1) * data[i].atr;
			data[i].add4 = data[i].add1 + ((type==TURTLE_BUY)?1:-1) * data[i].atr * 1.5;
			data[i].del1 = data[i+1].del1;
		}
	}

	// del2
	for(i=datanum-1; i>=0; i--)
		data[i].del2 = BreakOut(data, datanum, i, BreakOutDelRange, (type==TURTLE_BUY)?(BREAKOUT_LOW):(BREAKOUT_HIGH));

	// Have & Money & Overwrite add1234 del1 price
	i = (BreakOutAddRange>BreakOutDelRange)?(BreakOutAddRange):(BreakOutDelRange);
	i = (i>atr+1)?(i):(atr+1);
	i = datanum - i - 1;

	for(; i>=0; i--)
	{
		data[i].have = data[i+1].have;
		data[i].money = data[i+1].money;
		
		if( type == TURTLE_BUY )
		{
			if( data[i].high >= data[i+1].add1 && data[i+1].have <= 0 )
			{
				data[i].money -= ((data[i+1].add1>=data[i].low)?data[i+1].add1:data[i].low) * (1000+FEE);
				data[i].del1 = data[i+1].add1 - data[i+1].atr * 2;
				data[i].have += 1;
			}
			if( data[i].high >= data[i+1].add2 && data[i+1].have <= 1 )
			{
				data[i].money -= ((data[i+1].add2>=data[i].low)?data[i+1].add2:data[i].low) * (1000+FEE);
				data[i].del1 = data[i+1].add2 - data[i+1].atr * 2;
				data[i].have += 1;
			}
			if( data[i].high >= data[i+1].add3 && data[i+1].have <= 2 )
			{
				data[i].money -= ((data[i+1].add3>=data[i].low)?data[i+1].add3:data[i].low) * (1000+FEE);
				data[i].del1 = data[i+1].add3 - data[i+1].atr * 2;
				data[i].have += 1;
			}
			if( data[i].high >= data[i+1].add4 && data[i+1].have <= 3 )
			{
				data[i].money -= ((data[i+1].add4>=data[i].low)?data[i+1].add4:data[i].low) * (1000+FEE);
				data[i].del1 = data[i+1].add4 - data[i+1].atr * 2;
				data[i].have += 1;
			}

			if( data[i].have > 0 )
			{
				d1 = (data[i+1].del1>data[i+1].del2) ? (data[i+1].del1) : (data[i+1].del2);
				if( data[i].low <= d1 )
				{
					data[i].money += ((d1<=data[i].high)?d1:data[i].high) * data[i].have * (1000-FEE-TAX);
					data[i].del1 = 0;
					data[i].have = 0;
				}
			}

			if( data[i].have > 0 )
			{
				data[i].add1 = data[i+1].add1;
				data[i].add2 = data[i+1].add2;
				data[i].add3 = data[i+1].add3;
				data[i].add4 = data[i+1].add4;
			}
		}
		else
		{
			if( data[i].low <= data[i+1].add1 && data[i+1].have <= 0 )
			{
//printf("1");
				data[i].money += ((data[i+1].add1<=data[i].high)?data[i+1].add1:data[i].high) * (1000-FEE-TAX);
				data[i].del1 = data[i+1].add1 + data[i+1].atr * 2;
				data[i].have += 1;
			}
			if( data[i].low <= data[i+1].add2 && data[i+1].have <= 1 )
			{
//printf("2");
				data[i].money += ((data[i+1].add2<=data[i].high)?data[i+1].add2:data[i].high) * (1000-FEE-TAX);
				data[i].del1 = data[i+1].add2 + data[i+1].atr * 2;
				data[i].have += 1;
			}
			if( data[i].low <= data[i+1].add3 && data[i+1].have <= 2 )
			{
//printf("3");
				data[i].money += ((data[i+1].add3<=data[i].high)?data[i+1].add3:data[i].high) * (1000-FEE-TAX);
				data[i].del1 = data[i+1].add3 + data[i+1].atr * 2;
				data[i].have += 1;
			}
			if( data[i].low <= data[i+1].add4 && data[i+1].have <= 3 )
			{
//printf("4");
				data[i].money += ((data[i+1].add4<=data[i].high)?data[i+1].add4:data[i].high) * (1000-FEE-TAX);
				data[i].del1 = data[i+1].add4 + data[i+1].atr * 2;
				data[i].have += 1;
			}

			if( data[i].have > 0 )
			{
//printf("0");
				d1 = (data[i+1].del1<data[i+1].del2) ? (data[i+1].del1) : (data[i+1].del2);
				if( data[i].high >= d1 )
				{
					data[i].money -= ((d1>=data[i].low)?d1:data[i].low) * data[i].have * (1000+FEE);
					data[i].del1 = 9999;
					data[i].have = 0;
				}
			}

			if( data[i].have > 0 )
			{
				data[i].add1 = data[i+1].add1;
				data[i].add2 = data[i+1].add2;
				data[i].add3 = data[i+1].add3;
				data[i].add4 = data[i+1].add4;
			}
			
			if(data[i].del1 == 0 ) data[i].del1 = 9999;
//printf("\n");
		}
	}
}










/*




void turtle_sell(DATA data[], int datanum, int atr, int high, int low)
{
	int i, j;
	double d1, d2, d3;

	for(i=0; i<datanum; i++)
	{
		data[i].money = 0;
		data[i].have = 0;
		data[i].tr = 0;
		data[i].atr = 0;
		data[i].dayhigh = 0;
		data[i].daylow = 0;
		data[i].buy1 = 0;
		data[i].buy2 = 0;
		data[i].buy3 = 0;
		data[i].sell = 0;
	}
	
	// TR
	for(i=datanum-2; i>=0; i--)
	{
		d1 = (data[i].high > data[i].low)?(data[i].high - data[i].low):(data[i].low - data[i].high);
		d2 = (data[i].high > data[i+1].close)?(data[i].high - data[i+1].close):(data[i+1].close - data[i].high);
		d3 = (data[i].low > data[i+1].close)?(data[i].low - data[i+1].close):(data[i+1].close - data[i].low);

		data[i].tr = (d1>d2)?d1:d2;
		data[i].tr = (data[i].tr>d3)?data[i].tr:d3;
	}

	// ATR
	for(i=datanum-2; i>=datanum-1-atr; i--)
		data[datanum-1-atr].atr += data[i].tr;
	data[datanum-1-atr].atr /= atr;
	for(i=datanum-2-atr; i>=0; i--)
		data[i].atr = ( data[i+1].atr * (atr-1) + data[i].tr ) / atr;

	// DayHigh
	for(i=datanum-high; i>=0; i--)
	{
		for(j=i+high-1; j>=i; j--)
			data[i].dayhigh = (data[j].close>data[i].dayhigh) ? data[j].close : data[i].dayhigh;
	}

	// DayLow & buy123 & sell
	for(i=datanum-low; i>=0; i--)
	{
		data[i].daylow = 9999;
		for(j=i+low-1; j>=i; j--)
			data[i].daylow = (data[j].close<data[i].daylow) ? data[j].close : data[i].daylow;
		data[i].buy1 = data[i].daylow - data[i].atr * 0.5;
		data[i].buy2 = data[i].daylow - data[i].atr;
		data[i].buy3 = data[i].daylow - data[i].atr * 1.5;
		data[i].sell = data[i].daylow + data[i].atr * 2;
	}

	// Have & Money & Overwrite buy sell price
	i = (high>low)?(high):(low);
	i = (i>atr+1)?(i):(atr+1);
	i = datanum - i - 1;

	for(; i>=0; i--)
	{
		data[i].have = data[i+1].have;
		data[i].money = data[i+1].money;
		
		if( data[i].low <= data[i+1].daylow && data[i+1].have <= 0 )
		{
			data[i].money += data[i+1].daylow * 1000;
			data[i].have += 1;
		}
		if( data[i].low <= data[i+1].buy1 && data[i+1].have <= 1 )
		{
			data[i].money += data[i+1].buy1 * 1000;
			data[i].have += 1;
		}
		if( data[i].low <= data[i+1].buy2 && data[i+1].have <= 2 )
		{
			data[i].money += data[i+1].buy2 * 1000;
			data[i].have += 1;
		}
		if( data[i].low <= data[i+1].buy3 && data[i+1].have <= 3 )
		{
			data[i].money += data[i+1].buy3 * 1000;
			data[i].have += 1;
		}

		d1 = (data[i+1].sell<data[i+1].dayhigh) ? (data[i+1].sell) : (data[i+1].dayhigh);
		if( data[i].high >= d1 )
		{
			data[i].money -= d1 * 1000 * data[i].have;
			data[i].have = 0;
		}

		if( data[i].have > 0 )
		{
			data[i].daylow = data[i+1].daylow;
			data[i].buy1 = data[i+1].buy1;
			data[i].buy2 = data[i+1].buy2;
			data[i].buy3 = data[i+1].buy3;
			data[i].sell = data[i+1].sell;
		}
	}
}
*/
